//
//  ViewController.m
//  动漫之家
//
//  Created by 王少泽 on 16/11/17.
//  Copyright © 2016年 王少泽. All rights reserved.
//

#import "ViewController.h"
#import "ComicShowViewController.h"
//#import "RecommendViewController.h"
//#import "UpDateViewController.h"
//#import "ClassificationViewController.h"
//#import "RankViewController.h"
//#import "SpecialViewController.h"


#import "comicCollectionViewCell.h"
#import "XZBaseNetwork.h"


#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height
@interface ViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
//定义页面的属性

//



@end

@implementation ViewController
{
    UITableView* _comicView;
    UICollectionView* _comicClassView;
    NSArray* _imageArr;
    NSMutableArray* _dataArr;
    NSMutableDictionary* _dataDic;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    //进行网络请求
    
    self.navigationController.navigationBarHidden = YES;
//    [[XZBaseNetwork shareInstance] startWithGetURL:@"http://japi.juhe.cn/comic/category?key=e4c2c6f92b486c97e01328b1bc54c913" params:nil success:^(id success) {
//        
//        
//        NSLog(@"successDic:%@",success);
//        _dataArr = [success objectForKey:@"result"];
//        NSLog(@"%@",_dataArr);
//        [_comicClassView reloadData];
//        
//    } failure:^(id failure) {
//        
//    }];
    self.tabBarController.tabBar.hidden = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tabBarController.tabBar.hidden = NO;
    
    /*
     
        预留位置进行自定义的navigation
     
     */
    self.navigationController.navigationBar.barStyle = UIBarStyleDefault;
    self.navigationController.navigationBar.backgroundColor = [UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:0.1];
    
    //在导航条上添加searchbarcontroller
    UIBarButtonItem* searchBarItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:@selector(pushSearchPage:)];
    self.navigationItem.rightBarButtonItem = searchBarItem;
    
    
    [self lazyLoadView];
    [self.view addSubview:_comicClassView];

    
   
    
    
}
#pragma mark collectionView的懒加载模式
- (UICollectionView* )lazyLoadView
{
    if (_comicClassView == nil) {
        //初始化UICollectionView的FlowLayout
        UICollectionViewFlowLayout* flowLayout = [[UICollectionViewFlowLayout alloc]init];
        
        //设置itemSize
        flowLayout.itemSize = CGSizeMake(self.view.frame.size.width, 150);
        
        //初始化collectionView
        _comicClassView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height - 49 - 64) collectionViewLayout:flowLayout];
        _comicClassView.backgroundColor = [UIColor whiteColor];
        _comicClassView.backgroundView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"backView.jpg"]];
        //给网格注册单元格类
        [_comicClassView registerClass:[comicCollectionViewCell class] forCellWithReuseIdentifier:@"cellID"];
        //注册headerView
        [_comicClassView registerClass:[UICollectionViewCell class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"ruse"];
        _comicClassView.alwaysBounceVertical = YES;
        _comicClassView.delegate = self;
        _comicClassView.dataSource = self;
    }
    
    return _comicClassView;
}

#pragma mark collectionView的代理方法实现
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    comicCollectionViewCell* cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellID" forIndexPath:indexPath];
    
    
    cell.comicName.text = _dataArr[indexPath.row];
    NSLog(@"cell.text:%@",_dataArr[indexPath.row]);
    return cell;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 4;
}

//设置每个item的尺寸大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(self.view.frame.size.width, 110);
}

//设置item之间的间距

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 10;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 15;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"%@",_dataArr[indexPath.row]);
    
    ComicShowViewController* comicShow = [[ComicShowViewController alloc]init];
    comicShow.comicType = _dataArr[indexPath.row];
    
    [self.navigationController pushViewController:comicShow animated:YES];
    
    
}

#pragma mark 搜索控制器的方法
- (void)pushSearchPage:(UIBarButtonItem* )sender
{
    NSLog(@"进入搜索方法");
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
